package com.linwt.controller;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


import com.linwt.bean.Record;


import com.linwt.service.RecordService;
import com.linwt.service.VisitorService;
import com.linwt.util.Result;

import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;




@Validated
@RestController
@ResponseBody
@Api(tags="登记表数据接口")
public class RecordController {
	@Autowired
	VisitorService visitorService;
	@Autowired
	RecordService recordService;
	/**
	 * 
	 * @param recordMap
	 * @return
	 * @throws ParseException
	 */
	@PostMapping(value="/record/add",produces="application/json;charset=UTF-8")
	@ApiOperation(value="新增一条访客登记记录")
	@Transactional(propagation = Propagation.REQUIRED)
	public Result addRecord(
			@NotBlank(message="姓名不能为空")
			@Size(min=2,max=16,message="姓名长度介于2到16个字符")
			@RequestParam(name="visName") String visName,
		
			@NotBlank(message="性别不能为空")
			@RequestParam(name="visGender") String visGender,
			
			@NotBlank(message="手机号码不能为空")
			@Size(min=11,max=11,message="手机长度介于2到16个字符")
			@RequestParam(name="visPhone") String visPhone,
			
			@NotBlank(message="事由不能为空")
			@Size(min=3,max=100,message="事由长度介于10到100个字符")
			@RequestParam(name="visReason") String visReason
			) throws ParseException{
		Result result = new Result();
		//由系统产生来访时间
		Date date = new Date();
		
	
		//查询来访人记录
		String visId = visitorService.findVisitorByNameAndPhone(visName, visPhone);
		 //System.out.println("查询到visitId "+visitId);
		if(visId == null) {
			// 查询不到,新增一个visitor
			// 将时间作为visitId一部分
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMddHHmmss");
			String timeStr = sdf2.format(date);
			visId =  (String) visitorService.addVisitor(visName,visGender, visPhone,timeStr);
			//System.out.println("查询不到，新增visitId "+visitId);
		}
		
		//int a = 1/0; //模拟异常
		
		// 创建来访一条登记记录
		Record record = new Record();
		record.setId(date.getTime()+"");
		record.setVisId(visId);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = sdf.format(date);
		record.setTime(time);
		record.setVisReason(visReason);
		recordService.addRecord(record);
		
		result.setCode(0);
		result.setMsg("成功");
		result.setData("0");
		
		return result;
	}
	
	@GetMapping("record/query-all")
	@ApiOperation(value="查询所有的来访登记记录")
	@Transactional(propagation = Propagation.REQUIRED)
	public Result listAllRecords() {
		Result result = new Result();
		List<Record> records = recordService.listAllRecords();
		result.setCode(0);
		result.setData(records);
		result.setMsg("0");
		return result;
	}
	
	

	
}
